package com.xywl.escort.adapter;

import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xywl.escort.R;
import com.xywl.escort.bean.ProjectOrderListResp;
import com.xywl.escort.utils.NumberFormatUtils;
import com.xywl.escort.widget.CircleProgress;
import com.xywl.escort.widget.GlideApp;

import java.util.List;

public class SubscriptionOrderAdapter extends BaseQuickAdapter<ProjectOrderListResp.DataBean.ListBean, BaseViewHolder> {
    public SubscriptionOrderAdapter(int layoutResId, @Nullable List<ProjectOrderListResp.DataBean.ListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ProjectOrderListResp.DataBean.ListBean item) {
        ImageView imageView = helper.getView(R.id.img_project);
        GlideApp.with(mContext)
                .load(item.coverUrl)
                .centerCrop()
                .placeholder(R.mipmap.icon_default_picture)
                .into(imageView);

        helper.setText(R.id.text_title, item.name);
        helper.setText(R.id.text_time, item.subscribeBeginTime);
        helper.setText(R.id.text_hold_copies, "持有份数: " + item.holdNum + "份");

        // 1-认购中 2-售罄 3-派单 4-待认购 5-成长中 6-分红 7-项目失败 -1-未知（前端不显示状态）
        CircleProgress circleProgress = helper.getView(R.id.circleProgress);
        circleProgress.setMax(100);
        TextView textSecondLine = helper.getView(R.id.text_second_line);
        TextView textDividend = helper.getView(R.id.text_dividend);
        Button btnOrderShip = helper.getView(R.id.btn_order_ship);
        Button btnOrderRepurchase = helper.getView(R.id.btn_order_repurchase);
        switch (item.projectStatus) {
            case 1: {
                helper.setText(R.id.text_first_line, item.available + "份");
                textSecondLine.setVisibility(View.VISIBLE);
                textSecondLine.setText("剩余");
                int subscribeRate = (int) item.subscribeRate;
                circleProgress.setProgress(subscribeRate);
                btnOrderShip.setVisibility(View.GONE);
                btnOrderRepurchase.setVisibility(View.GONE);
                setTextValue(helper, item);
                textDividend.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(item.shareOutBonusTime)) {
                    String time = "预计分红: " + item.shareOutBonusTime;
                    textDividend.setText(time);
                }
            }
            break;
            case 2: {
                helper.setText(R.id.text_first_line, "售罄");
                textSecondLine.setVisibility(View.GONE);
                circleProgress.setProgress(100);
                btnOrderShip.setVisibility(View.GONE);
                btnOrderRepurchase.setVisibility(View.GONE);
                setTextValue(helper, item);
                textDividend.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(item.shareOutBonusTime)) {
                    String time = "预计分红: " + item.shareOutBonusTime;
                    textDividend.setText(time);
                }
            }
            break;
            case 3: {
                helper.setText(R.id.text_first_line, "派单");
                textSecondLine.setVisibility(View.GONE);
                circleProgress.setProgress(100);
                btnOrderShip.setVisibility(View.GONE);
                btnOrderRepurchase.setVisibility(View.GONE);
                setTextValue(helper, item);
                textDividend.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(item.shareOutBonusTime)) {
                    String time = "预计分红: " + item.shareOutBonusTime;
                    textDividend.setText(time);
                }
            }
            break;
            case 4: {
                helper.setText(R.id.text_first_line, "待认购");
                circleProgress.setProgress(0);
                btnOrderShip.setVisibility(View.GONE);
                btnOrderRepurchase.setVisibility(View.GONE);
                setTextValue(helper, item);
                textDividend.setVisibility(View.GONE);
            }
            break;
            case 5: {
                helper.setText(R.id.text_first_line, "成长中");
                textSecondLine.setVisibility(View.GONE);
                circleProgress.setProgress(100);
                btnOrderShip.setVisibility(View.GONE);
                btnOrderRepurchase.setVisibility(View.GONE);
                setTextValue(helper, item);
                textDividend.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(item.shareOutBonusTime)) {
                    String time = "分红时间: " + item.shareOutBonusTime;
                    textDividend.setText(time);
                }
            }
            break;
            case 6: {
                helper.setText(R.id.text_first_line, "分红");
                textSecondLine.setVisibility(View.GONE);
                circleProgress.setProgress(100);
                btnOrderShip.setVisibility(View.VISIBLE);
                btnOrderRepurchase.setVisibility(View.VISIBLE);

                if (null != item.marketPriceYuan) {
                    String textCount = "市场价/份 " + NumberFormatUtils.getInstance().format(item.marketPriceYuan.doubleValue()) + "元";
                    SpannableString spannableStringCount = setText(textCount, textCount.indexOf("份") + 1, textCount.length());
                    helper.setText(R.id.text_count, spannableStringCount);
                }
                if (null != item.buybackPriceYuan) {
                    String textPrice = "收购价/份 " + NumberFormatUtils.getInstance().format(item.buybackPriceYuan.doubleValue()) + "元";
                    SpannableString spannableStringCount = setText(textPrice, textPrice.indexOf("份") + 1, textPrice.length());
                    helper.setText(R.id.text_price, spannableStringCount);
                }

                textDividend.setVisibility(View.GONE);
            }
            break;
            case 7: {
                helper.setText(R.id.text_first_line, "项目");
                textSecondLine.setVisibility(View.VISIBLE);
                textSecondLine.setText("失败");
                circleProgress.setProgress(100);
                btnOrderShip.setVisibility(View.GONE);
                btnOrderRepurchase.setVisibility(View.GONE);
                setTextValue(helper, item);
                textDividend.setVisibility(View.GONE);
            }
            break;
            default:
                break;
        }

        helper.addOnClickListener(R.id.btn_order_ship);
        helper.addOnClickListener(R.id.btn_order_repurchase);
        helper.addOnClickListener(R.id.image_more);
    }

    private void setTextValue(BaseViewHolder helper, ProjectOrderListResp.DataBean.ListBean item) {
        if (null != item.subscribePriceYuan) {
            String textCount = "认购价/份" + NumberFormatUtils.getInstance().format(item.subscribePriceYuan.doubleValue()) + "元";
            SpannableString spannableStringCount = setText(textCount, textCount.indexOf("份") + 1, textCount.length());
            helper.setText(R.id.text_count, spannableStringCount);
        }
        String textPrice = "认购周期 " + item.subscribeCycle + "天";
        SpannableString spannableStringCount = setText(textPrice, textPrice.indexOf("期") + 1, textPrice.length());
        helper.setText(R.id.text_price, spannableStringCount);
    }

    private SpannableString setText(String text, int start, int end) {
        SpannableString spanString = new SpannableString(text);
        //再构造一个改变字体颜色的Span
        ForegroundColorSpan span = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.aid_prompt_color));
        //将这个Span应用于指定范围的字体
        spanString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        AbsoluteSizeSpan span1 = new AbsoluteSizeSpan(15, true);
        spanString.setSpan(span1, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanString;
    }
}
